<template>
	<view class="getPhone">
		<view class="title">展大狮小程序申请</view>
		<button class="getPhoneBtn" :disabled="disabled" @getphonenumber="getPhone" open-type="getPhoneNumber">授权绑定微信手机号</button>
		<view class="marginTop10 box">
			<view>
				<checkbox-group @change="checkboxChange">
					<label>
						<checkbox />我已阅读并同意<text class="xy"  @tap="getnav(router(`userAgreement`))">《用户协议》</text>
					</label>
				</checkbox-group>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				disabled: true,
				encrypteData: '',
				session_key: '',
				openid: '',
				iv: '',
				checked:false
			}
		},
		onLoad(option) {
			this.gets();
		},
		methods: {
			checkboxChange(){
				this.checked=!this.checked
				this.disabled=!this.disabled
			},
			gets() {
				uni.login({
					provider: 'oauth',
					success: (res) => {
						if (res.errMsg = "login:ok") {
							this.http('/api/user/jscode2session', 'post', {
									code: res.code
								}, false)
								.then((res) => {
									this.session_key = res.data.session_key;
									this.openid = res.data.openid;
									
								})
						}
					},
					fail: (err) => {
						this.disabled = false;
						uni.hideLoading();
					}
				})
			},
			login() {
				if (this.openid != '') {
					let v={
						openid:this.openid
					}
					this.http('/api/user/login', 'post',v , false)
						.then((res) => {
							uni.setStorageSync('userinfo', res.data)
						})
				}
			},
			getPhone(e) {
				let data = {
					type: "1",
					encryptedData: e.target.encryptedData,
					iv: e.target.iv,
					session_key: this.session_key,
					openid: this.openid
				}
				this.http('/api/user/register', 'post', data, false)
					.then((res) => {
						if(res.sta==200){
							uni.showToast({
								title:'绑定成功',
								icon:'none'
							});
							this.disabled = true;
							this.login()
							uni.switchTab({
								url: this.router(`index`)
							})
						}else{
							uni.showToast({
								title:'取消授权',
								icon:'none'
							});
						}
					})

				// console.log(e)
				// uni.showToast({
				// 	title:'请稍后...',
				// 	icon:'none'
				// });
				// this.disabled = true;
				// if (e.detail.errMsg == "getPhoneNumber:ok") {
				// 	this.encrypteData = e.detail.encryptedData;
				// 	this.iv = e.detail.iv;
				// 	this.http('apicloud/Login/getsession_key','post',{code:this.code},false).then( (res) => {
				// 		if (res.status == 200) {
				// 			this.session_key = res.data.session_key.session_key;
				// 			return this.session_key
				// 		}
				// 	}).then( (session_key) => {
				// 		this.disabled = false;
				// 		uni.hideLoading();
				// 		if (session_key != '') {
				// 			let data = {
				// 				session_key: this.session_key,
				// 				encrypteData: this.encrypteData,
				// 				iv: this.iv,
				// 				token:this.token,
				// 				extension_id:uni.getStorageSync('extension_id') == ''?'':uni.getStorageSync('extension_id')
				// 			}
				// 			this.http('apicloud/Login/eidt_user_phone','post',data,false).then(res => {
				// 				if (res.status == 200) {
				// 					uni.switchTab({
				// 						url:this.router(`index`)
				// 					});
				// 					uni.setStorageSync('user',res.data.user);
				// 					this.disabled = true;
				// 					uni.hideLoading();
				// 				}else{
				// 					uni.showToast({
				// 						title:res.mess,
				// 						icon:'none'
				// 					})
				// 					this.disabled = true;
				// 					uni.hideLoading();
				// 				}

				// 			})
				// 		}
				// 	})
				// }else{
				// 	uni.showToast({
				// 		title:'用户已取消',
				// 		icon:'none'
				// 	})
				// 	this.disabled = false;
				// }
			}
		}
	}
</script>

<style lang="less">
	.getPhone {
		padding: 0 40rpx;
		.xy{
			color:#2291FF  ;
		}
		.title {
			text-align: center;
			padding: 100rpx 0;
			font-size: 36rpx;
			font-weight: bold;
			color: rgba(51, 51, 51, 1);
		}

		.getPhoneBtn {
			width: 100%;
			height: 100rpx;
			color: #fff;
			font-size: 36rpx;
			line-height: 100rpx;
			background: #2291FF !important;
			border-radius: 5px;
		}

		.box {
			// color: #2291FF;
		}

		.iconfont {
			color: #2291FF;
		}
	}
</style>
